package distriKub;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;
import javax.swing.ListModel;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import java.util.Random;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GamePanel extends javax.swing.JFrame {
	
	// Game parameters
	public static int turnTime = 30; // In seconds
	// End of game parameters

	private final int NOTSELECTED = -1;
	Component tileFromMove = null;
	Component tileToMove = null;
	int moveTileFromIndex = NOTSELECTED; // The index of the tile to move
	int moveTileToIndex = NOTSELECTED; // Where should the tile moved be put
	Myself boundPlayer;
	InfoPanel boundInfoPanel;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JPanel currentHand;
	private JLabel lbl_SuggestedTile;
	private JLabel lbl_Opponents;
	private JLabel lbl_IncommingMessages;
	private JButton chatSendMessage;
	private JTextField chatOutgoingMessages;
	private JTextPane chatIncommingMessages;
	private JList chatOpponentsList;
	private JButton additionalTile;
	private JButton drawFromBankButton;
	private JButton doneTurnButton;
	private JButton checkVictoryButton;
	private AbstractAction buttonClicked;
	private JLabel lbl_TurnTimer;
	private JProgressBar turnTimer;
	private JLabel lbl_CurrentRack;
	private JPanel additionalTilePanel;
	private JButton tile13;
	private JButton tile12;
	private JButton tile11;
	private JButton tile10;
	private JButton tile9;
	private JButton tile8;
	private JButton tile7;
	private JButton tile6;
	private JButton tile5;
	private JButton tile4;
	private JButton tile3;
	private JButton tile2;
	private JButton tile1;

	/**
	* Auto-generated main method to display this JFrame
	*/
	
	public GamePanel() { // Constructor
		super();
		initGUI();
		this.setResizable(false);
		this.setAlwaysOnTop(false);
		
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		} // End of catch block
		
		BufferedImage image = null;
        try {
            image = ImageIO.read(this.getClass().getResource("/distriKub\\images\\appicon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.setIconImage(image);
        
        addWindowListener(
        		new WindowAdapter() {
        			public void windowClosing( WindowEvent e ){
        				if (JOptionPane.showConfirmDialog (null,"Are you sure ?")==JOptionPane.YES_OPTION) {
        					   boundPlayer.closeAllCommunications();
        			           boundInfoPanel.setVisible(false);
        			           boundInfoPanel.dispose();
        			           boundPlayer.myRegPlayerPanel.setVisible(false);
        			           boundPlayer.myRegPlayerPanel.dispose();
        			           setVisible(false);
        			           dispose();
        			           }
        			} // End of windowClosing
        		}); // End of addWindowListener
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
			getContentPane().setBackground(new java.awt.Color(232,232,0));
			this.setTitle("DistriKub - Personal Game Panel");
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			this.setName("gameFrame");
			getContentPane().setForeground(new java.awt.Color(255,128,0));
			{
				currentHand = new JPanel();
				currentHand.setName("currentHand");
				currentHand.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				currentHand.setBackground(new java.awt.Color(128,128,255));
				{
					tile1 = new JButton();
					currentHand.add(tile1);
					tile1.setPreferredSize(new java.awt.Dimension(66, 71));
					tile1.setBackground(new java.awt.Color(255,255,255));
					tile1.setFont(new java.awt.Font("Tahoma",1,36));
					tile1.setName("1");
					tile1.setAction(getButtonClicked());
					tile1.setHideActionText(true);
					tile1.setText("1");
					tile1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile2 = new JButton();
					currentHand.add(tile2);
					tile2.setText("2");
					tile2.setBackground(new java.awt.Color(255,255,255));
					tile2.setFont(new java.awt.Font("Tahoma",1,36));
					tile2.setPreferredSize(new java.awt.Dimension(66, 71));
					tile2.setName("2");
					tile2.setAction(getButtonClicked());
					tile2.setHideActionText(true);
					tile2.setText("2");
					tile2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile3 = new JButton();
					currentHand.add(tile3);
					tile3.setText("3");
					tile3.setBackground(new java.awt.Color(255,255,255));
					tile3.setFont(new java.awt.Font("Tahoma",1,36));
					tile3.setPreferredSize(new java.awt.Dimension(66, 71));
					tile3.setName("3");
					tile3.setAction(getButtonClicked());
					tile3.setHideActionText(true);
					tile3.setText("3");
					tile3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile4 = new JButton();
					currentHand.add(tile4);
					tile4.setText("4");
					tile4.setBackground(new java.awt.Color(255,255,255));
					tile4.setFont(new java.awt.Font("Tahoma",1,36));
					tile4.setPreferredSize(new java.awt.Dimension(66, 71));
					tile4.setName("4");
					tile4.setAction(getButtonClicked());
					tile4.setHideActionText(true);
					tile4.setText("4");
					tile4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile5 = new JButton();
					currentHand.add(tile5);
					tile5.setText("5");
					tile5.setBackground(new java.awt.Color(255,255,255));
					tile5.setFont(new java.awt.Font("Tahoma",1,36));
					tile5.setPreferredSize(new java.awt.Dimension(66, 71));
					tile5.setName("5");
					tile5.setAction(getButtonClicked());
					tile5.setHideActionText(true);
					tile5.setText("5");
					tile5.setAction(getButtonClicked());
					tile5.setHideActionText(true);
					tile5.setText("5");
					tile5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile6 = new JButton();
					currentHand.add(tile6);
					tile6.setText("6");
					tile6.setBackground(new java.awt.Color(255,255,255));
					tile6.setFont(new java.awt.Font("Tahoma",1,36));
					tile6.setPreferredSize(new java.awt.Dimension(66, 71));
					tile6.setName("6");
					tile6.setAction(getButtonClicked());
					tile6.setHideActionText(true);
					tile6.setText("6");
					tile6.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile7 = new JButton();
					currentHand.add(tile7);
					tile7.setText("7");
					tile7.setBackground(new java.awt.Color(255,255,255));
					tile7.setFont(new java.awt.Font("Tahoma",1,36));
					tile7.setPreferredSize(new java.awt.Dimension(66, 71));
					tile7.setName("7");
					tile7.setAction(getButtonClicked());
					tile7.setHideActionText(true);
					tile7.setText("7");
					tile7.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile8 = new JButton();
					currentHand.add(tile8);
					tile8.setText("8");
					tile8.setBackground(new java.awt.Color(255,255,255));
					tile8.setFont(new java.awt.Font("Tahoma",1,36));
					tile8.setPreferredSize(new java.awt.Dimension(66, 71));
					tile8.setName("8");
					tile8.setAction(getButtonClicked());
					tile8.setHideActionText(true);
					tile8.setText("8");
					tile8.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile9 = new JButton();
					currentHand.add(tile9);
					tile9.setText("9");
					tile9.setBackground(new java.awt.Color(255,255,255));
					tile9.setFont(new java.awt.Font("Tahoma",1,36));
					tile9.setPreferredSize(new java.awt.Dimension(66, 71));
					tile9.setName("9");
					tile9.setAction(getButtonClicked());
					tile9.setHideActionText(true);
					tile9.setText("9");
					tile9.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile10 = new JButton();
					currentHand.add(tile10);
					tile10.setText("10");
					tile10.setBackground(new java.awt.Color(255,255,255));
					tile10.setFont(new java.awt.Font("Tahoma",1,36));
					tile10.setPreferredSize(new java.awt.Dimension(66, 71));
					tile10.setName("10");
					tile10.setAction(getButtonClicked());
					tile10.setHideActionText(true);
					tile10.setText("10");
					tile10.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile11 = new JButton();
					currentHand.add(tile11);
					tile11.setText("11");
					tile11.setBackground(new java.awt.Color(255,255,255));
					tile11.setFont(new java.awt.Font("Tahoma",1,36));
					tile11.setPreferredSize(new java.awt.Dimension(66, 71));
					tile11.setName("11");
					tile11.setAction(getButtonClicked());
					tile11.setHideActionText(true);
					tile11.setText("11");
					tile11.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile12 = new JButton();
					currentHand.add(tile12);
					tile12.setText("12");
					tile12.setBackground(new java.awt.Color(255,255,255));
					tile12.setFont(new java.awt.Font("Tahoma",1,36));
					tile12.setPreferredSize(new java.awt.Dimension(66, 71));
					tile12.setName("12");
					tile12.setAction(getButtonClicked());
					tile12.setHideActionText(true);
					tile12.setText("12");
					tile12.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
				{
					tile13 = new JButton();
					currentHand.add(tile13);
					tile13.setText("13");
					tile13.setBackground(new java.awt.Color(255,255,255));
					tile13.setFont(new java.awt.Font("Tahoma",1,36));
					tile13.setPreferredSize(new java.awt.Dimension(66, 71));
					tile13.setName("13");
					tile13.setAction(getButtonClicked());
					tile13.setHideActionText(true);
					tile13.setText("13");
					tile13.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				}
			}
			{
				lbl_SuggestedTile = new JLabel();
				lbl_SuggestedTile.setText("Suggested Tile");
				lbl_SuggestedTile.setHorizontalAlignment(SwingConstants.CENTER);
				lbl_SuggestedTile.setFont(new java.awt.Font("Tahoma",1,11));
				lbl_SuggestedTile.setForeground(new java.awt.Color(0,0,0));
			}
			{
				additionalTilePanel = new JPanel();
				additionalTilePanel.setBackground(new java.awt.Color(227,254,41));
				additionalTilePanel.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				additionalTilePanel.setName("additionalTilePanel");
				additionalTilePanel.add(getAdditionalTile());
			}
			{
				turnTimer = new JProgressBar();
				turnTimer.setOrientation(SwingConstants.VERTICAL);
				turnTimer.setBackground(Color.white);
				turnTimer.setMaximum(GamePanel.turnTime);
				turnTimer.setValue(turnTime);
				turnTimer.setString("0");
				turnTimer.setForeground(Color.green);
				turnTimer.setToolTipText("Got this much time left");
			}
			{
				lbl_TurnTimer = new JLabel();
				lbl_TurnTimer.setText("Timer");
				lbl_TurnTimer.setHorizontalAlignment(SwingConstants.CENTER);
				lbl_TurnTimer.setFont(new java.awt.Font("Tahoma",1,11));
				lbl_TurnTimer.setForeground(new java.awt.Color(0,0,0));
			}
			{
				lbl_CurrentRack = new JLabel();
				lbl_CurrentRack.setText("Current Rack");
				lbl_CurrentRack.setHorizontalAlignment(SwingConstants.CENTER);
				lbl_CurrentRack.setFont(new java.awt.Font("Tahoma",1,11));
				lbl_CurrentRack.setForeground(new java.awt.Color(0,0,0));
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(lbl_CurrentRack, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(currentHand, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				        .addComponent(getCheckVictoryButton(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
				        .addComponent(getDoneTurnButton(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
				        .addComponent(getThrowTileButton(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				            .addComponent(getLbl_IncommingMessages(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				            .addComponent(getLbl_Opponents(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(getChatMessagesScrollPane(), GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 29, Short.MAX_VALUE))
				            .addGroup(thisLayout.createSequentialGroup()
				                .addComponent(getChatOpponentsList(), GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 0, Short.MAX_VALUE))
				            .addGroup(thisLayout.createSequentialGroup()
				                .addGroup(thisLayout.createParallelGroup()
				                    .addComponent(lbl_SuggestedTile, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				                    .addComponent(lbl_TurnTimer, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				                .addGroup(thisLayout.createParallelGroup()
				                    .addGroup(thisLayout.createSequentialGroup()
				                        .addComponent(additionalTilePanel, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
				                        .addGap(0, 0, Short.MAX_VALUE))
				                    .addGroup(thisLayout.createSequentialGroup()
				                        .addComponent(turnTimer, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
				                        .addGap(0, 0, Short.MAX_VALUE))
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addGap(0, 45, Short.MAX_VALUE)
				                        .addComponent(getChatSendMessage(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				                        .addComponent(getChatOutgoingMessages(), GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)))))))
				.addContainerGap(22, 22));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(currentHand, GroupLayout.PREFERRED_SIZE, 1022, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 12, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addPreferredGap(currentHand, getCheckVictoryButton(), LayoutStyle.ComponentPlacement.INDENT)
				        .addComponent(getCheckVictoryButton(), GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addComponent(getDoneTurnButton(), GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addComponent(getThrowTileButton(), GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
				        .addGap(18)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(thisLayout.createSequentialGroup()
				                .addGroup(thisLayout.createParallelGroup()
				                    .addComponent(getChatOpponentsList(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addGap(8)
				                        .addComponent(getLbl_Opponents(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				                        .addGap(6)))
				                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				                .addGroup(thisLayout.createParallelGroup()
				                    .addComponent(getChatMessagesScrollPane(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 260, GroupLayout.PREFERRED_SIZE)
				                    .addComponent(getChatOutgoingMessages(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 260, GroupLayout.PREFERRED_SIZE)))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(lbl_CurrentRack, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
				                .addGap(55)
				                .addComponent(getLbl_IncommingMessages(), GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
				                .addGap(96)))
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addComponent(getChatSendMessage(), GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(thisLayout.createSequentialGroup()
				                .addGap(0, 0, Short.MAX_VALUE)
				                .addComponent(lbl_TurnTimer, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addGap(0, 7, Short.MAX_VALUE)
				                .addComponent(turnTimer, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
				                .addGap(9)))
				        .addGap(31)
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(additionalTilePanel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
				            .addComponent(lbl_SuggestedTile, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE))))
				.addContainerGap(20, 20));
			pack();
			this.setSize(1072, 301);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	boolean additionalTileSelected = false;
	public void tileClicked(JButton tile) {
		if (tile.equals(additionalTilePanel.getComponent(0))) { // Additional tile was selected
			if (additionalTileSelected==false) {
			additionalTileSelected = true;
			tile.setBackground(Color.MAGENTA);
			return;
			} else {
				additionalTileSelected = false;
				tile.setBackground(Color.WHITE);
				return;
			} // End of else block
		}
		if ((!tile.equals(additionalTilePanel.getComponent(0)))&&additionalTileSelected==true) { // Player want to put the additional tile in the rack
			if (tile.getText().equals("")) { // Trying to replace a joker
				JOptionPane.showMessageDialog(null, "Hey wise guy, keep the joker !");
				return; // No point in replacing a joker - this is something to consider handling in a real-life game version
			}
			
			for (Tile theTile : boundPlayer.myBank.gameBank) // Throwing the replaced tile
				if (theTile.equals(tile)){
					theTile.suggest();
					theTile.setTileOwnerId(Tile.BANKID);
				}
			
			
			tile.setText(additionalTile.getText());
			tile.setForeground(additionalTile.getForeground());
			tile.setBackground(Color.WHITE);
			additionalTile.setVisible(false);
			additionalTileSelected = false;
			return; // TODO: Before return, need to call a throwTile method with the tile being replaced
		} // End of if block
		
		if (moveTileFromIndex==NOTSELECTED) {
			tile.setBackground(Color.MAGENTA);
			for (int currentTile = 0;currentTile<13;currentTile++) {
				if ( tile.equals((JButton)currentHand.getComponent(currentTile))) {
					moveTileFromIndex = currentTile;
				} // End of internal if block 
			} // End of for block
		} // End of external if block
			
			else { // A tile to move was already selected
				for (int currentTile = 0;currentTile<13;currentTile++) {
					if (tile.equals((JButton)currentHand.getComponent(currentTile))){
						moveTileToIndex = currentTile;
					} // End of if block
				} // End of for block
						currentHand.add(currentHand.getComponent(moveTileFromIndex), moveTileToIndex);
						((JButton)currentHand.getComponent(moveTileToIndex)).setBackground(Color.white);
						currentHand.validate();
						currentHand.repaint();
						moveTileFromIndex = NOTSELECTED;
						moveTileToIndex = NOTSELECTED;
					} // End of else block
	}// End of tileClicked method
	
	public void setTimerValue(int value) {
		this.turnTimer.setValue(value);
		if (value>turnTime*0.7)
			turnTimer.setForeground(Color.green);
		if (value<turnTime*0.7 && value>turnTime*0.5)
			turnTimer.setForeground(Color.orange);
		if (value<turnTime*0.5)
			turnTimer.setForeground(Color.red);		
		this.turnTimer.repaint();
	}
	
	public int[] getNewTilesOrder() {
		int[] newOrder = new int[13];
		for (int currentTile = 0;currentTile<13;currentTile++) {
			newOrder[currentTile] = Integer.parseInt(((JButton)currentHand.getComponent(currentTile)).getName());
		}
		return newOrder;
	}
			
	private AbstractAction getButtonClicked() {
		if(buttonClicked == null) {
			buttonClicked = new AbstractAction("", null) {
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public void actionPerformed(ActionEvent evt) {
					tileClicked((JButton)evt.getSource());
				}
			};
		}
		return buttonClicked;
	}
	
	Action chatSendClicked = new AbstractAction() {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			boundInfoPanel.displaySystemMessage("Send chat message to : " + chatOpponentsList.getSelectedValue().toString()); // TODO: Remove this line
			boundPlayer.sendChatFromGUI(boundPlayer.screenName + " says : " + chatOutgoingMessages.getText(), chatOpponentsList.getSelectedValue().toString());
		}
		
	};
	
	ImageIcon victoryIcon = new ImageIcon("distriKub\\images\\victory.jpg");
	Action checkWiningClicked = new AbstractAction("",victoryIcon) {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			boundInfoPanel.displaySystemMessage("Victory !"); // TODO: Realize the actual action here
			
		}
	};
	
	ImageIcon doneIcon = new ImageIcon("distriKub\\images\\done.jpg");
	Action doneTurnClicked = new AbstractAction("",doneIcon) {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			boundInfoPanel.displaySystemMessage("Done with my turn !"); // TODO: Realize the actual action here
			boundPlayer.updateBankFromGUI();
			boundPlayer.stopThePlayTimer();
			boundPlayer.circulateBank();
		}
		
	};
	
	ImageIcon bankIcon = new ImageIcon("distriKub\\images\\bank.jpg");
	private JScrollPane ChatMessagesScrollPane;
	Action drawFromBankClicked = new AbstractAction("",bankIcon) {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			boundInfoPanel.displaySystemMessage("A request to draw from the bank !"); // TODO: Remove this line
			Random randomPlaceInBank = new Random(Bank.NUMBEROFTILES);
			randomPlaceInBank.setSeed(System.currentTimeMillis());
			int currentPlaceInBank = randomPlaceInBank.nextInt(Bank.NUMBEROFTILES);
			while (Tile.BANKID==boundPlayer.myBank.gameBank[currentPlaceInBank].getTileOwnerId() && currentPlaceInBank<boundPlayer.myBank.gameBank.length)
				{
				currentPlaceInBank = randomPlaceInBank.nextInt(Bank.NUMBEROFTILES);
				boundInfoPanel.displaySystemMessage(""+currentPlaceInBank);
				}
			boundPlayer.myBank.gameBank[currentPlaceInBank].setTileOwnerId(boundPlayer.getId());
			setAdditionalTile(boundPlayer.myBank.gameBank[currentPlaceInBank]);
			setBankButtonAvailability(false);
		}
		
	};

	private JButton getCheckVictoryButton() {
		if(checkVictoryButton == null) {
			checkVictoryButton = new JButton();
			checkVictoryButton.setAction(checkWiningClicked);
			checkVictoryButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("distriKub/images/victory.jpg")));
			checkVictoryButton.setBorder(new LineBorder(new java.awt.Color(0,0,0), 2, true));
			checkVictoryButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			checkVictoryButton.setToolTipText("Declare your win !");
		}
		return checkVictoryButton;
	}
	
	private JButton getDoneTurnButton() {
		if(doneTurnButton == null) {
			doneTurnButton = new JButton();
			doneTurnButton.setAction(doneTurnClicked);
			doneTurnButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("distriKub/images/done.jpg")));
			doneTurnButton.setBorder(new LineBorder(new java.awt.Color(0,0,0),2,true));
			doneTurnButton.setToolTipText("Done with my turn");
			doneTurnButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		}
		return doneTurnButton;
	}
	
	private JButton getThrowTileButton() {
		if(drawFromBankButton == null) {
			drawFromBankButton = new JButton();
			drawFromBankButton.setAction(drawFromBankClicked);
			drawFromBankButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("distriKub/images/bank.jpg")));
			drawFromBankButton.setBorder(new LineBorder(new java.awt.Color(0,0,0),2,true));
			drawFromBankButton.setToolTipText("Draw another tile from the bank");
			drawFromBankButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		}
		return drawFromBankButton;
	}
	
	public void setTiles(Tile[] privateRack) {
		for (Tile theTile : privateRack) {
			((JButton)currentHand.getComponent(theTile.getTilePosition())).setText(String.valueOf(theTile.getTileNumber()));
			
			Color tileColor=null;
			
			switch (theTile.getTileColor()) {
			case Black : tileColor = Color.black; break;
			case Blue : tileColor = Color.blue; break;
			case Yellow : tileColor = Color.orange; break; // Orange on white looks yellow while yellow looks bad
			case Red : tileColor = Color.red; break;
			case Joker : tileColor = Color.pink; break;
			} // End of switch block
			
			if (theTile.getTileColor() == Tile.tileColors.Joker) {
				((JButton)currentHand.getComponent(theTile.getTilePosition())).setText(""); // TODO: Make sure the change of the text didn't affect the hand updating
				((JButton)currentHand.getComponent(theTile.getTilePosition())).setIcon(new ImageIcon(getClass().getClassLoader().getResource("distriKub/images/joker.jpg")));
			}
			
			((JButton)currentHand.getComponent(theTile.getTilePosition())).setForeground(tileColor);						
		} // End of for block
	}
	
	public JButton[] getFinalRack(){ // Returns the final state of the player's rack.
		JButton[] finalRack = new JButton[13];
		for (int positionInFinalRack=0;positionInFinalRack<currentHand.getComponentCount();positionInFinalRack++)
			finalRack[positionInFinalRack] = (JButton)currentHand.getComponent(positionInFinalRack);
		return finalRack;
	}
	
	public void setAdditionalTile(Tile theTile){
		additionalTile.setText(String.valueOf(theTile.getTileNumber()));
		Color tileColor=null;
		
		switch (theTile.getTileColor()) {
		case Black : tileColor = Color.black; break;
		case Blue : tileColor = Color.blue; break;
		case Yellow : tileColor = Color.orange; break; // Orange on white looks yellow while yellow looks bad
		case Red : tileColor = Color.red; break;
		case Joker : tileColor = Color.pink; break;
		} // End of switch block
		
		additionalTile.setForeground(tileColor);
		
		if (theTile.getTileColor() == Tile.tileColors.Joker) {
			additionalTile.setText(""); // TODO: Make sure the change of the text didn't affect the hand updating
			additionalTile.setIcon(new ImageIcon(getClass().getClassLoader().getResource("distriKub/images/joker.jpg")));
			additionalTile.repaint();
		}
	}
	
	public JButton getAdditionalTile(){
		
	additionalTile = new JButton();
	additionalTile.setAction(getButtonClicked());
	additionalTile.setHideActionText(true);
	additionalTile.setBackground(new java.awt.Color(255,255,255));
	additionalTile.setFont(new java.awt.Font("Tahoma",1,36));
	additionalTile.setPreferredSize(new java.awt.Dimension(66, 71));
	additionalTile.setName("additionalTile");
	additionalTile.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	additionalTile.setSize(66, 71);
	return additionalTile;
	}
	
	public void setChatMessage(String theMessage){ // Checked OK
		this.chatIncommingMessages.setText(chatIncommingMessages.getText() + "\n" + theMessage);
		getChatIncommingMessages().selectAll();
	}
	
	public void bindToPlayer(Myself thePlayer){
		this.boundPlayer = thePlayer;
	}
	
	public void bindToInfoPanel(InfoPanel theInfoPanel){
		this.boundInfoPanel = theInfoPanel;
	}
	
	public void setBankButtonAvailability(boolean state){
		drawFromBankButton.setEnabled(state);
	}
	
	public void setTurnButtonAvailability(boolean state){
		doneTurnButton.setEnabled(state);
	}
	
	private JList getChatOpponentsList() {
		if(chatOpponentsList == null) {
			chatOpponentsList = new JList();
			chatOpponentsList.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0,0,0)), "", TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION));
			chatOpponentsList.setSelectedIndex(0);
		}
		return chatOpponentsList;
	}
	
	public String getSelectedOpponentForChat(){
		chatOpponentsList.setSelectedIndex(0);
		return chatOpponentsList.getSelectedValue().toString();
	}
	
	private JTextPane getChatIncommingMessages() {
		if(chatIncommingMessages == null) {
			chatIncommingMessages = new JTextPane();
			chatIncommingMessages.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			chatIncommingMessages.setEditable(false);
			chatIncommingMessages.setPreferredSize(new java.awt.Dimension(280, 81));
		}
		return chatIncommingMessages;
	}
	
	private JTextField getChatOutgoingMessages() {
		if(chatOutgoingMessages == null) {
			chatOutgoingMessages = new JTextField();
			chatOutgoingMessages.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
		}
		return chatOutgoingMessages;
	}
	
	private JButton getChatSendMessage() {
		if(chatSendMessage == null) {
			chatSendMessage = new JButton();
			chatSendMessage.setAction(chatSendClicked);
			chatSendMessage.setText("Send");
			chatSendMessage.setBackground(new java.awt.Color(255,255,128));
			chatSendMessage.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		}
		return chatSendMessage;
	}
	
	private JLabel getLbl_IncommingMessages() {
		if(lbl_IncommingMessages == null) {
			lbl_IncommingMessages = new JLabel();
			lbl_IncommingMessages.setText("Chat Messages");
			lbl_IncommingMessages.setFont(new java.awt.Font("Tahoma",1,11));
		}
		return lbl_IncommingMessages;
	}
	
	private JLabel getLbl_Opponents() {
		if(lbl_Opponents == null) {
			lbl_Opponents = new JLabel();
			lbl_Opponents.setText("Opponents");
			lbl_Opponents.setFont(new java.awt.Font("Tahoma",1,11));
		}
		return lbl_Opponents;
	}
	
	public void updateOpponentsInGUI(String[] opponentNames){
		ListModel chatOpponentsListModel = new DefaultComboBoxModel(opponentNames);
		chatOpponentsList.setModel(chatOpponentsListModel);
		chatOpponentsList.setSelectedIndex(0);
	}
	
	private JScrollPane getChatMessagesScrollPane() {
		if(ChatMessagesScrollPane == null) {
			ChatMessagesScrollPane = new JScrollPane();
			ChatMessagesScrollPane.setAutoscrolls(true);
			ChatMessagesScrollPane.setViewportView(getChatIncommingMessages());
		}
		return ChatMessagesScrollPane;
	}

}
